package com.ruoyi.epidemic.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.epidemic.domain.EpidemicControlDept;
import com.ruoyi.epidemic.domain.WxCpSysUser;
import com.ruoyi.epidemic.mapper.EpidemicControlDeptMapper;
import com.ruoyi.epidemic.service.IEpidemicControlDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.text.Convert;

import static com.ruoyi.common.utils.ShiroUtils.getLoginName;

/**
 * 疫情防控部门Service业务层处理
 *
 * @author ruoyi
 * @date 2022-04-05
 */
@Service
public class EpidemicControlDeptServiceImpl implements IEpidemicControlDeptService
{
    @Autowired
    private EpidemicControlDeptMapper epidemicControlDeptMapper;

    /**
     * 查询疫情防控部门
     *
     * @param deptId 疫情防控部门主键
     * @return 疫情防控部门
     */
    @Override
    public EpidemicControlDept selectEpidemicControlDeptByDeptId(Long deptId)
    {
        return epidemicControlDeptMapper.selectEpidemicControlDeptByDeptId(deptId);
    }

    /**
     * 查询疫情防控部门列表
     *
     * @param epidemicControlDept 疫情防控部门
     * @return 疫情防控部门
     */
    @Override
    public List<EpidemicControlDept> selectEpidemicControlDeptList(EpidemicControlDept epidemicControlDept)
    {
        return epidemicControlDeptMapper.selectEpidemicControlDeptList(epidemicControlDept);
    }

    /**
     * 新增疫情防控部门
     *
     * @param epidemicControlDept 疫情防控部门
     * @return 结果
     */
    @Override
    public int insertEpidemicControlDept(EpidemicControlDept epidemicControlDept)
    {
        EpidemicControlDept info = epidemicControlDeptMapper.selectDeptById(epidemicControlDept.getParentId());
        // 如果父节点不为"正常"状态,则不允许新增子节点
        if (!UserConstants.DEPT_NORMAL.equals(info.getStatus()))
        {
            throw new ServiceException("部门停用，不允许新增");
        }
        epidemicControlDept.setCreateTime(DateUtils.getNowDate());
        epidemicControlDept.setCreateBy(getLoginName());
        return epidemicControlDeptMapper.insertEpidemicControlDept(epidemicControlDept);
    }

    /**
     * 修改疫情防控部门
     *
     * @param epidemicControlDept 疫情防控部门
     * @return 结果
     */
    @Override
    public int updateEpidemicControlDept(EpidemicControlDept epidemicControlDept)
    {
        epidemicControlDept.setUpdateTime(DateUtils.getNowDate());
        epidemicControlDept.setUpdateBy(getLoginName());
        return epidemicControlDeptMapper.updateEpidemicControlDept(epidemicControlDept);
    }

    /**
     * 批量删除疫情防控部门
     *
     * @param deptIds 需要删除的疫情防控部门主键
     * @return 结果
     */
    @Override
    public int deleteEpidemicControlDeptByDeptIds(String deptIds)
    {
        return epidemicControlDeptMapper.deleteEpidemicControlDeptByDeptIds(Convert.toStrArray(deptIds));
    }

    /**
     * 删除疫情防控部门信息
     *
     * @param deptId 疫情防控部门主键
     * @return 结果
     */
    @Override
    public int deleteEpidemicControlDeptByDeptId(Long deptId)
    {
        return epidemicControlDeptMapper.deleteEpidemicControlDeptByDeptId(deptId);
    }

    /**
     * 疫情防疫部门树
     * @param epidemicControlDept
     * @return
     */
    @Override
    public List<Ztree> selectEpidemicDeptTree(EpidemicControlDept epidemicControlDept) {
        List<EpidemicControlDept> epidemicControlDeptList = epidemicControlDeptMapper.selectEpidemicControlDeptList(epidemicControlDept);
        List<Ztree> ztreeList = initZtree(epidemicControlDeptList);
        return ztreeList;
    }

    /**
     * 对象转部门树
     *
     * @param epidemicControlDeptList 疫情防疫部门列表
     * @return 树结构列表
     */
    public List<Ztree> initZtree(List<EpidemicControlDept> epidemicControlDeptList)
    {
        return initZtree(epidemicControlDeptList, null);
    }

    /**
     * 对象转部门树
     *
     * @param epidemicControlDeptList 疫情防疫部门列表
     * @param roleDeptList 角色已存在菜单列表
     * @return 树结构列表
     */
    public List<Ztree> initZtree(List<EpidemicControlDept> epidemicControlDeptList, List<String> roleDeptList)
    {

        List<Ztree> ztrees = new ArrayList<Ztree>();
        boolean isCheck = StringUtils.isNotNull(roleDeptList);
        for (EpidemicControlDept dept : epidemicControlDeptList)
        {
            if (UserConstants.DEPT_NORMAL.equals(dept.getStatus()))
            {
                Ztree ztree = new Ztree();
                ztree.setId(dept.getDeptId());
                ztree.setpId(dept.getParentId());
                ztree.setName(dept.getDeptName());
                ztree.setTitle(dept.getDeptName());
                if (isCheck)
                {
                    ztree.setChecked(roleDeptList.contains(dept.getDeptId() + dept.getDeptName()));
                }
                ztrees.add(ztree);
            }
        }
        return ztrees;
    }

    @Override
    public List<WxCpSysUser> selectEpidemicUserList(WxCpSysUser wxCpSysUser) {
        return epidemicControlDeptMapper.selectEpidemicUserList(wxCpSysUser);
    }

    @Override
    public WxCpSysUser selectEpidemicUser(Long userDeptId) {
        return epidemicControlDeptMapper.selectEpidemicUser(userDeptId);
    }

    @Override
    public List<Map<String, Object>> getAllWxCpSysUser(String deptId) {
        return epidemicControlDeptMapper.getAllWxCpSysUser(deptId);
    }

    @Override
    public EpidemicControlDept selectDeptById(Long deptId) {
        return epidemicControlDeptMapper.selectDeptById(deptId);
    }

    @Override
    public int selectDeptCount(Long parentId) {
        EpidemicControlDept dept = new EpidemicControlDept();
        dept.setParentId(parentId);
        return epidemicControlDeptMapper.selectDeptCount(dept);
    }

    @Override
    public boolean checkDeptExistUser(Long deptId) {
        int result = epidemicControlDeptMapper.checkDeptExistUser(deptId);
        return result > 0 ? true : false;
    }

    @Override
    public String checkDeptNameUnique(EpidemicControlDept dept) {
        Long deptId = StringUtils.isNull(dept.getDeptId()) ? -1L : dept.getDeptId();
        EpidemicControlDept info = epidemicControlDeptMapper.checkDeptNameUnique(dept.getDeptName(), dept.getParentId());
        if (StringUtils.isNotNull(info) && info.getDeptId().longValue() != deptId.longValue())
        {
            return UserConstants.DEPT_NAME_NOT_UNIQUE;
        }
        return UserConstants.DEPT_NAME_UNIQUE;
    }

    @Override
    public int selectNormalChildrenDeptById(Long deptId) {
        return epidemicControlDeptMapper.selectNormalChildrenDeptById(deptId);
    }

    /**
     * 根据父ID查询下一层子部门（正常状态）
     *
     * @param deptId 部门ID
     * @return 子部门数
     */
    @Override
    public List<EpidemicControlDept> selectDeptByparentId(Long deptId, String flag){
        return epidemicControlDeptMapper.selectDeptByparentId(deptId, flag);
    }

    /**
     * 根据父ID查询所有子部门（正常状态）
     *
     * @param deptId 部门ID
     * @return 子部门数
     */
    @Override
    public String selectAllDeptByrootId(Long deptId){
        return epidemicControlDeptMapper.selectAllDeptByrootId(deptId);
    }

    @Override
    public List<EpidemicControlDept> selectDeptByparentIdForExternal(Long deptId, String flag) {
        return epidemicControlDeptMapper.selectDeptByparentIdForExternal(deptId, flag);
    }
}
